package com.bw.guoych;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Pdf2picDemo {
    public static void main(String[] args){
        File file = new File("f:\\正则表达式简明参考.pdf");
        try {
            PDDocument doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for (int i = 0; i < pageCount; i++) {
                // 方式1,第二个参数是设置缩放比(即像素)
                BufferedImage image = renderer.renderImageWithDPI(i, 296);
                // 方式2,第二个参数是设置缩放比(即像素)
                // BufferedImage image = renderer.renderImage(i, 2.5f);
                File f = new File("f:/result/正则表达式简明参考" + i + ".jpg");
                if (!f.getParentFile().exists()){
                    f.getParentFile().mkdirs();
                }
                ImageIO.write(image, "JPG", f);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("wnabi");
    }
}
